package com.lanou.controller;

import com.lanou.entity.Certificate;
import com.lanou.service.CertificateService;
import com.lanou.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName CertificateController
 * @Description TODO
 * @Author chenzhaobao
 * @Date 2025年08月14日 9:57
 * @Version 1.0
 */
@RestController
@RequestMapping("/certificate")
public class CertificateController {
    @Autowired
    private CertificateService certificateService;
    @PostMapping
    public R insertCertificate(@RequestBody Certificate certificate) {
        int i = certificateService.insertCertificate(certificate);
        if (i> 0) {
            return R.ok("新增证书成功");
        } else {
            return R.error("新增证书失败");
        }
    }
    @DeleteMapping("/{id}")
    public R deleteCertificateById(@PathVariable Integer id) {
        int i = certificateService.deleteCertificateById(id);
        if (i > 0) {
            return R.ok("删除证书成功");
        }else {
            return R.error("删除证书失败");
        }
    }
    @PutMapping
    public R updateCertificate(@RequestBody Certificate certificate) {
        int i = certificateService.updateCertificate(certificate);
        if (i > 0) {
            return R.ok("更新证书成功");
        }else {
            return R.error("更新证书失败");
        }
    }
    @GetMapping("/{id}")
    public R getCertificateById(@PathVariable Integer id) {
        Certificate certificate = certificateService.getCertificateById(id);
        if (certificate != null) {
            return R.ok(certificate);
        }else {
            return R.error("查询证书失败");
        }
    }
    @GetMapping
    public R getAllCertificates() {
        List<Certificate> certificates = certificateService.getAllCertificates();
        if (certificates != null && certificates.size() > 0) {
            return R.ok(certificates);
        }else {
            return R.error("查询证书失败");
        }
    }

    @GetMapping("/consultant/{consultantId}")
    public R getCertificatesByConsultantId(@PathVariable Integer consultantId) {
        List<Certificate> certificates = certificateService.getCertificatesByConsultantId(consultantId);
        if (certificates != null && certificates.size() > 0) {
            return R.ok(certificates);
        }else {
            return R.error("查询证书失败");
        }
    }
}
